package com.dxl.lanju.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.service.ShopService;
import com.dxl.web.common.controller.BaseController;
import com.lanju.bean.ShopBean;
import com.lanju.entity.Shop;
import com.lanju.enums.ShopType;

/**
 * 门店信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/shop")
@RequirePermission(value={"SHOP-MODULE"}, name="门店模块", desc="包括门店增、删、改、查")
public class ShopController extends BaseController {
	private static final Logger LOG = Logger.getLogger(ShopController.class);
	@Autowired
	private ShopService shopService;
	
	@RequestMapping("")
	@RequirePermission(value={"SHOP-VIEW"}, name="门店查看", desc="查看门店信息")
	public ModelAndView shop() {
		ModelAndView mv = new ModelAndView("shop/list");
		return mv;
	}
	
	@RequestMapping("/audit")
	@RequirePermission(value={"SHOP-AUDIT"}, name="门店审核", desc="门店审核")
	public ModelAndView shopStatus() {
		ModelAndView mv = new ModelAndView("shop/audit");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"SHOP-VIEW"}, name="门店查看", desc="查看门店信息")
	public Page<ShopBean> list(String keyword, String type, String areaId, Integer state, Integer page, Integer rows) {
		Page<ShopBean> p = this.shopService.list(keyword, type, areaId, state, page, rows);
		return p;
	}
	
	@ResponseBody
	@RequestMapping("/listAudit")
	@RequirePermission(value={"SHOP-AUDIT"}, name="门店审核", desc="门店审核")
	public Page<ShopBean> listAudit(String keyword, String type, String areaId, Integer state, Integer status, Integer page, Integer rows) {
		Page<ShopBean> p = this.shopService.listAudit(keyword, type, areaId, state, status, page, rows);
		return p;
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"SHOP-VIEW"}, name="门店查看", desc="查看门店信息")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView("shop/edit");
		if (Utils.isNotBlank(id)) {
			Shop shop = this.baseService.get(Shop.class, id);
			mv.addObject("entity", shop);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"SHOP-MODIFY"}, name="门店修改", desc="修改门店信息")
	public RespData save(Shop shop) {
		return shopService.save(shop);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"SHOP-DEL"}, name="门店删除", desc="删除门店信息")
	public RespData del(String[] ids) {
		return shopService.del(ids);
	}
	
	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"SHOP-STATE"}, name="门店状态修改", desc="修改门店状态")
	public RespData state(String[] ids, Integer state) {
		return this.state(Shop.class, ids, state);
	}
	
	@ResponseBody
	@RequestMapping("/status")
	@RequirePermission(value={"SHOP-AUDIT"}, name="门店审核", desc="门店审核")
	public RespData status(String[] ids, Integer status) {
		if (Utils.isEmpty(ids) || Utils.isNull(status)) {
			return RespData.instance(RespData.FAIL);
		} else {
			try {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("status", status);
				this.baseService.update(Shop.class, ids, params);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	@ResponseBody
	@RequestMapping("/shoptype")
	public List<Map<String, Object>> shoptype() {
		List<Map<String, Object>> types = new ArrayList<Map<String, Object>>();
		ShopType[] values = ShopType.values();
		Map<String, Object> item;
		for (ShopType value : values) {
			item = new HashMap<String, Object>();
			item.put("type", value.getType());
			item.put("name", ShopType.getTypeName(value.getType()));
			types.add(item);
		}
		return types;
	}
}
